﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace MopsesPetShop
{
    public partial class Pay : System.Web.UI.Page
    {
        //protected void PlusMinusClick(object sender,EventArgs e)
        //{
        //    var i = int.Parse(((HtmlGenericControl) ((Button) sender).Parent.Controls[1]).InnerText);
        //    i += (((Button) sender).Text == "+" ? 1 : -1);
        //    ((HtmlGenericControl) ((Button) sender).Parent.Controls[1]).InnerText = i.ToString();
        //}

        private float _startSumm;

        protected void CreateRow(DataClasses1DataContext dc, HtmlGenericControl div, bool isAcc, int ekInstance)
        {

            var box = new HtmlGenericControl();
            box.Style.Add(HtmlTextWriterStyle.Display, "block");
            box.Style.Add(HtmlTextWriterStyle.MarginTop, "50px");
            var fullName = new HtmlGenericControl();
            if (isAcc)
            {
                var instance = dc.AccInstances.Where(i => i.pk == ekInstance).First();
                var kind = dc.AccKinds.Where(k => k.pk == instance.ekKind).First();
                fullName.InnerText = kind.kind + ":" + instance.name;
            }
            else
            {
                var instance = dc.Instances.Where(i => i.pk == ekInstance).First();
                var kind = dc.Kinds.Where(k => k.pk == instance.ekKind).First();
                fullName.InnerText = kind.kind + ":" + instance.name;
            }
            //fullName.Style.Add(HtmlTextWriterStyle.Width, "500px");
            //fullName.Style.Add(HtmlTextWriterStyle.MarginRight, "-500px");
            //fullName.Style.Add("float", "left");
            fullName.Style.Add(HtmlTextWriterStyle.Display, "block");
            box.Controls.Add(fullName);
            var pluses = new Dictionary<HtmlButton, List<HtmlInputReset>>();
            var minuses = new Dictionary<HtmlButton, List<HtmlInputReset>>();
            //var pluses = new Dictionary<HtmlInputReset, HtmlButton>();
            //var minuses = new Dictionary<HtmlInputReset, HtmlButton>();
            var right = new HtmlGenericControl();

            //right.Style.Add(HtmlTextWriterStyle.MarginLeft,"300px");
            var costDiv = new HtmlGenericControl();
            //costDiv.Style.Add(HtmlTextWriterStyle.Display, "block");
            var label = new HtmlGenericControl
                            {
                                InnerText = "Цена = ",
                            };
            costDiv.Controls.Add(label);
            var cost = new HtmlInputReset();
            cost.Style.Add(HtmlTextWriterStyle.Width, "100px");
            cost.Value = (isAcc ? dc.AccInstances.Where(accIns => accIns.pk == ekInstance).First().cost : dc.Instances.Where(ins => ins.pk == ekInstance).First().cost).ToString();
            cost.Value = cost.Value.Replace(',', '.');
            cost.Disabled = true;
            costDiv.Controls.Add(cost);
            right.Controls.Add(costDiv);
            if (isAcc)
            {
                var buttonsDiv = new HtmlGenericControl();
                //buttonsDiv.Style.Add(HtmlTextWriterStyle.Display,"block");
                var mult = new HtmlGenericControl
                               {
                                   InnerText = "*"
                               };
                mult.Style.Add(HtmlTextWriterStyle.MarginLeft, "20px");
                mult.Style.Add(HtmlTextWriterStyle.MarginRight, "20px");
                buttonsDiv.Controls.Add(mult);
                var minus = new HtmlButton
                                {
                                    InnerText = "-",
                                };
                minuses.Add(minus, new List<HtmlInputReset>());

                var count = new HtmlInputReset
                                {
                                    Value = "1",
                                    Disabled = true
                                    //ID = "num1"
                                };
                count.Style.Add(HtmlTextWriterStyle.Width, "50px");
                var plus = new HtmlButton
                               {
                                   InnerText = "+",
                               };
                pluses.Add(plus, new List<HtmlInputReset>());
                pluses[plus].Add(cost);
                minuses[minus].Add(cost);
                pluses[plus].Add(count);
                minuses[minus].Add(count);

                buttonsDiv.Controls.Add(minus);
                buttonsDiv.Controls.Add(count);
                buttonsDiv.Controls.Add(plus);
                right.Controls.Add(buttonsDiv);

                var payDiv = new HtmlGenericControl();
                //payDiv.Style.Add(HtmlTextWriterStyle.Display, "block");
                label = new HtmlGenericControl
                {
                    InnerText = " = ",
                };
                payDiv.Controls.Add(label);
                var pay = new HtmlInputReset();
                pay.Style.Add(HtmlTextWriterStyle.Width, "100px");
                pay.Value = (isAcc ? dc.AccInstances.Where(accIns => accIns.pk == ekInstance).First().cost : dc.Instances.Where(ins => ins.pk == ekInstance).First().cost).ToString();
                pay.Disabled = true;
                payDiv.Controls.Add(pay);

                pluses[plus].Add(pay);
                minuses[minus].Add(pay);

                right.Controls.Add(payDiv);

                _startSumm += int.Parse(count.Value) * float.Parse((isAcc
                                   ? dc.AccInstances.Where(accIns => accIns.pk == ekInstance).First().cost
                                   : dc.Instances.Where(ins => ins.pk == ekInstance).First().cost).ToString());
            }
            else
            {
                _startSumm += float.Parse((isAcc
                                   ? dc.AccInstances.Where(accIns => accIns.pk == ekInstance).First().cost
                                   : dc.Instances.Where(ins => ins.pk == ekInstance).First().cost).ToString());
            }
            box.Controls.Add(right);
            contentMainDiv.Controls.Add(box);
            foreach (var minuse in minuses)
            {
                var textEvent = "onclick";
                var code = "return MinusClick(";
                foreach (var reset in minuse.Value)
                {
                    code += "'" + reset.Name + "',";
                }
                code = code.Substring(0, code.Length - 1);
                code += ")";
                minuse.Key.Attributes.Add(textEvent, code);
            }
            foreach (var pluse in pluses)
            {
                var textEvent = "onclick";
                var code = "return PlusClick(";
                foreach (var reset in pluse.Value)
                {
                    code += "'" + reset.Name + "',";
                }
                code = code.Substring(0, code.Length - 1);
                code += ")";
                pluse.Key.Attributes.Add(textEvent, code);
            }
            //foreach (var minus in minuses)
            //    minus.Value.Attributes.Add("onclick", "return MinusClick('" + minus.Key.Name + "')");
            //foreach (var plus in pluses)
            //    plus.Value.Attributes.Add("onclick", "return PlusClick('" + plus.Key.Name + "')");
        }

        static HtmlGenericControl Adress(string label)
        {
            var ans = new HtmlGenericControl();
            ans.Style.Add(HtmlTextWriterStyle.Display, "block");
            var left = new HtmlGenericControl
                           {
                               InnerText = label.PadRight(15, '_')
                           };
            left.Style.Add(HtmlTextWriterStyle.FontFamily, "Consolas");
            var right = new HtmlInputText();
            ans.Controls.Add(left);
            ans.Controls.Add(right);
            return ans;
        }

        protected string StartSumm()
        {
            return _startSumm.ToString().Replace(',', '.');
        }



        protected void Page_Load(object sender, EventArgs e)
        {
            var dc = new DataClasses1DataContext();
            _startSumm = 0;
            miniTitle.InnerText = "Ваша корзина";
            if (Session["Pets"] != null)
                foreach (var pet in ((List<Instances>)Session["Pets"]))
                {
                    //CreateRow(dc, false, dc.Scopes.Where(sc => sc.scope == "Instances").First().pk, pet.ekPreview, kind.ekEntity, pet.ekKind, pet.pk, true);
                    CreateRow(dc, contentMainDiv, false, pet.pk);
                }
            if (Session["Acc"]!=null)
                foreach (var acc in ((List<AccInstances>)Session["Acc"]))
                {
                    //CreateBox(dc, true, dc.Scopes.Where(sc => sc.scope == "Instances").First().pk, acc.ekPreview, kind.ekEntity, acc.ekKind, acc.pk, true);
                    CreateRow(dc, contentMainDiv, true, acc.pk);
                }

            var connectDiv = new HtmlGenericControl();
            connectDiv.Style.Add(HtmlTextWriterStyle.Display, "block");
            connectDiv.Style.Add(HtmlTextWriterStyle.MarginTop, "100px");
            var region = new HtmlGenericControl();
            region.Style.Add(HtmlTextWriterStyle.Display, "block");
            var l = new HtmlGenericControl
                        {
                            InnerText = "Выберите регион "
                        };
            region.Controls.Add(l);
            var comboBox = new DropDownList();
            comboBox.Items.AddRange(new[]{
                                            new ListItem("Москва"), 
                                            new ListItem("Пермь"), 
                                        });
            region.Controls.Add(comboBox);
            connectDiv.Controls.Add(region);
            var radio1 = new RadioButton
                             {
                                 Text = "Срочная",
                                 GroupName = "speedGroup"
                             };
            radio1.Style.Add(HtmlTextWriterStyle.Display, "block");
            var radio2 = new RadioButton
                             {
                                 Text = "Не срочная",
                                 GroupName = "speedGroup",
                                 Checked = true
                             };
            radio2.Style.Add(HtmlTextWriterStyle.Display, "block");
            connectDiv.Controls.Add(radio1);
            connectDiv.Controls.Add(radio2);

            var adress = new HtmlGenericControl();
            adress.Controls.Add(Adress("Город:"));
            adress.Controls.Add(Adress("Улица:"));
            adress.Controls.Add(Adress("Дом:"));
            adress.Controls.Add(Adress("Квартира:"));
            adress.Controls.Add(Adress("e-mail:"));



            connectDiv.Controls.Add(adress);


            contentMainDiv.Controls.Add(connectDiv);
            dc.Connection.Close();

        }
    }
}